package com.study.ju.web;

import com.study.ju.model.req.User;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 * <p>添加类描述</p>
 *
 * @author chentao
 * @version v 0.1 2020/10/31 19:23
 */
@Controller
public class LoginController {
    @RequestMapping("/login")
    public String downloadTransferTempl(HttpSession session, HttpServletRequest request, HttpServletResponse response, @ModelAttribute("pro") User user, RedirectAttributes att) {
        if("a".equals(user.getName())){
            user.setId("a");
        }
        if("b".equals(user.getName())){
            user.setId("b");
        }
        request.getSession().setAttribute("user", user);
        att.addAttribute("username", user.getName());   //把username参数传递到request中
        att.addAttribute("userId", user.getId());
        return "redirect:/welcom.jsp";
    }
    @RequestMapping("/getUser")
    @ResponseBody
    public User getUser( HttpServletRequest request, HttpServletResponse response) {
        User user = (User) request.getSession().getAttribute("user");
        return user;
    }
}
